package com.byx.chatclient.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.byx.chatclient.R;
import com.byx.chatclient.bean.LoginDropDownBean;
import com.byx.chatclient.utils.GlideUtils;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

/**
 * AccountAdapter
 *
 * @author Superb
 * @date 2021/1/29 16:07
 */
public class LoginDropDownAdapter extends BaseAdapter {
    private Context context;
    private List<LoginDropDownBean> loginVos;
    private OnDeleteListener onDeleteListener;

    public LoginDropDownAdapter(Context context, List<LoginDropDownBean> loginVos) {
        this.context = context;
        this.loginVos = loginVos;
    }

    public void setLoginVos(List<LoginDropDownBean> loginVos) {
        this.loginVos = loginVos;
    }

    public void setOnDeleteListener(OnDeleteListener onDeleteListener) {
        this.onDeleteListener = onDeleteListener;
    }

    public void removeItem(int position){
        loginVos.remove(position);
        notifyDataSetChanged();
    }

    /**
     * How many items are in the data set represented by this Adapter.
     *
     * @return Count of items.
     */
    @Override
    public int getCount() {
        return loginVos.size();
    }

    /**
     * Get the data item associated with the specified position in the data set.
     *
     * @param position Position of the item whose data we want within the adapter's
     *                 data set.
     * @return The data at the specified position.
     */
    @Override
    public Object getItem(int position) {
        return loginVos.get(position);
    }

    /**
     * Get the row id associated with the specified position in the list.
     *
     * @param position The position of the item within the adapter's data set whose row id we want.
     * @return The id of the item at the specified position.
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * Get a View that displays the data at the specified position in the data set. You can either
     * create a View manually or inflate it from an XML layout file. When the View is inflated, the
     * parent View (GridView, ListView...) will apply default layout parameters unless you use
     * {@link LayoutInflater#inflate(int, ViewGroup, boolean)}
     * to specify a root view and to prevent attachment to the root.
     *
     * @param position    The position of the item within the adapter's data set of the item whose view
     *                    we want.
     * @param convertView The old view to reuse, if possible. Note: You should check that this view
     *                    is non-null and of an appropriate type before using. If it is not possible to convert
     *                    this view to display the correct data, this method can create a new view.
     *                    Heterogeneous lists can specify their number of view types, so that this View is
     *                    always of the right type (see {@link #getViewTypeCount()} and
     *                    {@link #getItemViewType(int)}).
     * @param parent      The parent that this view will eventually be attached to
     * @return A View corresponding to the data at the specified position.
     */
    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        LoginDropDownBean loginDropDownBean = loginVos.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_login_account_popup_item_view, parent,false);
            viewHolder = new ViewHolder();
            viewHolder.sdvPerson = convertView.findViewById(R.id.iv_person);
            viewHolder.textView = convertView.findViewById(R.id.tv_account);
            viewHolder.ivClear = convertView.findViewById(R.id.iv_clear);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.textView.setText(loginDropDownBean.getLoginId());
        viewHolder.sdvPerson.setImageURI(loginDropDownBean.getAvatar());
        if (onDeleteListener != null) {
            viewHolder.ivClear.setOnClickListener(v -> onDeleteListener.onItemDeleteClick(position));
        }

        return convertView;
    }

    public interface OnDeleteListener{
        /**
         * 点击列表项删除图标回调
         * @param position item索引
         */
        void onItemDeleteClick(int position);
    }

    private static class ViewHolder{
        public SimpleDraweeView sdvPerson;
        public TextView textView;
        public ImageView ivClear;
    }
}
